package com.example.customviewtest.wymusic;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.customviewtest.R;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @auther XuDan on 2020/4/29.
 */
public class DiskView extends RelativeLayout {
    private String image_pat = "";
    private Context context;
    private CircleImageView mDisk_layout_head_img;
    private ImageView mDisc_mg;
    private ImageView mDisk_layout_needle_img;
    private Animation mRotateAnimation;
    private Animation mNeedleGoAnim;
    private LinearInterpolator mLin;
    private boolean isPlaying = true;
    private Animation mNeedle_back_anim;

    public DiskView(Context context) {
        this(context, null);
    }

    public DiskView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiskView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.disk_view_layout, this, true);
        mDisc_mg = view.findViewById(R.id.disk_layout_disc_img);
        mDisk_layout_head_img = view.findViewById(R.id.disk_layout_head_img);
        mDisk_layout_needle_img = view.findViewById(R.id.disk_layout_needle_img);
        Glide.with(context).load(image_pat).into(mDisk_layout_head_img);
        startAnim(context);

    }

    private void startAnim(Context context) {
        mRotateAnimation = AnimationUtils.loadAnimation(context, R.anim.disc_anim);
        mLin = new LinearInterpolator();
        mRotateAnimation.setInterpolator(mLin);
        mDisk_layout_head_img.startAnimation(mRotateAnimation);

        mNeedleGoAnim = AnimationUtils.loadAnimation(context, R.anim.needle_go_anim);
        mRotateAnimation.setInterpolator(mLin);
        mDisk_layout_needle_img.startAnimation(mNeedleGoAnim);
    }

    private void stopAnim(Context context) {
        mRotateAnimation.cancel();
        mDisk_layout_head_img.clearAnimation();

        mNeedle_back_anim = AnimationUtils.loadAnimation(context, R.anim.needle_back_anim);
        mNeedle_back_anim.setInterpolator(mLin);
        mDisk_layout_needle_img.startAnimation(mNeedle_back_anim);
    }

    public void setImage_pat(String image_pat) {
        this.image_pat = image_pat;
        Glide.with(context).load(image_pat).into(mDisk_layout_head_img);
    }

    public void controlPlay() {
        if (isPlaying) {
            isPlaying = false;
            stopAnim(context);
        } else {
            isPlaying = true;
            startAnim(context);
        }
    }

    public void cleanAnim() {
        isPlaying = false;
        if (mRotateAnimation != null) {
            mRotateAnimation.cancel();
            mDisk_layout_head_img.clearAnimation();
        }
        if (mNeedleGoAnim != null) {
            mNeedleGoAnim.cancel();
            mDisk_layout_head_img.clearAnimation();
        }
        if (mNeedle_back_anim != null) {
            mNeedle_back_anim.cancel();
            mDisk_layout_head_img.clearAnimation();
        }
    }
}
